package com.yayan.beautyclub.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yayan.beautyclub.bean.User;
import com.yayan.beautyclub.service.IUserService;
import com.yayan.util.PropertiesUtil;
import com.yayan.util.RequestUtil;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value="/user", method=RequestMethod.POST)
public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/create.do")
	@ResponseBody
	public boolean createUser(@RequestBody User user){		
		long r = userService.saveUser(user.getOpenId());
		if(r == 0L)
			return false;
		return true;
	}
	
	@RequestMapping(value="/set_userInfo.do")
	@ResponseBody
	public boolean updateUser(@RequestBody User user){
		long uId = user.getuId();
		return userService.updateUser(uId, user);
	}
	
	/**
	 * 微信用户授权登陆接口
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/login.do")
	@ResponseBody
	public Map<String, Object> login(@RequestBody Map<String, String> map){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			String js_code = map.get("code");
			//读取配置文件信息，获取appid与secret
			PropertiesUtil pro = new PropertiesUtil("app.properties");
			//获取微信小程序的APPID
			String appid = pro.readValue("appid");
			//获取secret
			String secret = pro.readValue("secret");
			//拼接微信请求接口字符串
			String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid 
					+ "&secret="+ secret + "&js_code=" + js_code + "&grant_type=authorization_code";
			
			//以GET方法进行https协议请求，获取openId等信息
			String jsonStr = RequestUtil.httpsRequest(requestUrl, "GET", null);
			//将字符串转换成JSONObject类型
			JSONObject obj = JSONObject.parseObject(jsonStr);
			
			//根据openId获取用户信息
			User user = userService.findUserByopenId(obj.getString("openid"));
			//如果用户信息不存在则将openid存入数据库，并获取用户id，否则直接获取用户id。
			if(user == null){
				result.put("userid", userService.saveUser(obj.getString("openid")));
				//flag设为0，表示第一次插入该用户，此时可将获取的微信用户信息更新到数据库
				result.put("flag", 0);
			}else{
				result.put("userid", user.getuId());
				result.put("flag", 1);
			}
			result.put("status", true);
		}catch(Exception e){
			result.put("status", false);
		}
		result.put("isDefault", 0);
		return result;
	}
	
	@RequestMapping(value="/set_contactInfo.do")
	@ResponseBody
	public boolean setPhoneAndName(@RequestBody User user){
		long uId = user.getuId();
		return userService.setPhoneAndName(uId, user);
	}
	
	@RequestMapping(value="/get_user.do")
	@ResponseBody
	public User getUser(@RequestBody Map<String, String> map){
		long uId = Long.parseLong(map.get("uId"));
		return userService.findUserById(uId);
	}
	
	@RequestMapping(value="/user_list.do")
	@ResponseBody
	public List<User> getUserList(@RequestBody Map<String, String> map){
		int page = Integer.parseInt(map.get("page"));
		int size = Integer.parseInt(map.get("size"));
		return userService.findUserByCondition(page, size);	
	}

}
